//
//  YYEBindPhoneViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/2.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEBindPhoneViewController.h"
#import "JKCountDownButton.h"
#import "YYEMoreInfoViewController.h"
#import "YYEMineViewController.h"
#import "UIViewController+UIViewControllerExt.h"


@interface YYEBindPhoneViewController ()

@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) UITextField *veriTF;
@property (nonatomic,strong) UIButton *confirmBtn;

@end

@implementation YYEBindPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"绑定手机";
    
    [self configUI];
    
}

- (void)configUI {
    
    self.phoneTF = [[UITextField alloc]init];
    self.phoneTF.placeholder = @"请输入您的手机号码";
    [self.view addSubview:self.phoneTF];
    
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 43, 38)];
    self.phoneTF.leftView = phoneView;
    self.phoneTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *phoneIV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 9.5, 13, 19)];
    phoneIV.image = [UIImage imageNamed:@"phone"];
    [phoneView addSubview:phoneIV];
    
    JKCountDownButton *veriBtn = [[JKCountDownButton alloc]init];
    [veriBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [veriBtn setBackgroundColor:VeriBackColor];
    veriBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    veriBtn.layer.cornerRadius = 5;
    [self.view addSubview:veriBtn];
    
    self.veriTF = [[UITextField alloc]init];
    self.veriTF.placeholder = @"请输入您收到的验证码";
    [self.view addSubview:self.veriTF];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = LightLineColor;
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = LightLineColor;
    [self.view addSubview:line2];
    
    UIView *veriView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 43, 38)];
    self.veriTF.leftView = veriView;
    self.veriTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *veriIV = [[UIImageView alloc]initWithFrame:CGRectMake(8, 9.5, 17, 19)];
    veriIV.image = [UIImage imageNamed:@"password"];
    [veriView addSubview:veriIV];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmBtn setBackgroundColor:GeneralBlueColor];
    self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.confirmBtn.layer.cornerRadius = 5;
    [self.confirmBtn addTarget:self action:@selector(confirmClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    
    [self.phoneTF makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-130);
        make.top.equalTo(self.view.top).offset(20);
        make.height.equalTo(38);
    }];
    
    [veriBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.right).offset(-30);
        make.centerY.equalTo(self.phoneTF.centerY);
        make.size.equalTo(CGSizeMake(90, 26));
    }];
    
    [line1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-30);
        make.top.equalTo(self.phoneTF.bottom).offset(10);
        make.height.equalTo(0.5);
    }];
    
    [self.veriTF makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-30);
        make.top.equalTo(self.phoneTF.bottom).offset(20);
        make.height.equalTo(38);
    }];
    
    [line2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-30);
        make.top.equalTo(self.veriTF.bottom).offset(10);
        make.height.equalTo(0.5);
    }];
    
    [self.confirmBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(30);
        make.right.equalTo(self.view.right).offset(-30);
        make.centerY.equalTo(self.view.centerY);
        make.height.equalTo(42);
    }];
    
}

- (void)confirmClicked {
    
    NSMutableDictionary  *paramDic=[[NSMutableDictionary  alloc]initWithCapacity:0];
    
    if ([self.type isEqualToString:@"qq"]) {
        [paramDic  setValue:@"qq" forKey:@"type"];
        
    }
    else {
        [paramDic  setValue:@"weixin" forKey:@"type"];
    }
    [paramDic  setValue:self.openid forKey:@"openid"];
    [paramDic  setValue:self.sex forKey:@"sex"];
    [paramDic  setValue:self.name forKey:@"name"];
    [paramDic  setValue:self.iconurl forKey:@"iconurl"];
    [paramDic  setValue:self.phoneTF.text forKey:@"username"];
    [paramDic  setValue:self.veriTF.text forKey:@"verify"];
    //        [paramDic  setValue:[InfoCache unarchiveObjectWithFile:@"pushToken"] forKey:@"deviceToken"];
    //        [paramDic  setValue:@"ios" forKey:@"deviceType"];
    
    
    [SVProgressHUD show];
    [AFNHelper post:BindPhone parameters:paramDic success:^(id responseObject) {
        
        if ([responseObject[@"status"] isEqual:@0]) { // 注册/登录失败
            
            NSLog(@"登录失败");
            
        }else if ([responseObject[@"status"] isEqual:@1]) { // 登录成功
            
            PersonModel *model = [PersonModel yy_modelWithJSON:responseObject[@"data"][@"userinfo"]];
            [InfoCache archiveObject:model toFile:Person];
            
            [LoginModel shareLogin].token = responseObject[@"data"][@"token"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"token"], @"token");
            
            [LoginModel shareLogin].role = responseObject[@"data"][@"userinfo"][@"role_id"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"role_id"], @"role");
            
            [LoginModel shareLogin].uid = responseObject[@"data"][@"userinfo"][@"id"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"id"], @"uid");
            
            [LoginModel shareLogin].username = responseObject[@"data"][@"userinfo"][@"username"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"username"], @"username");
            
            [self popViewController:@"YYEMineViewController"];
//            [self.navigationController popViewControllerAnimated:YES];
            
        }else if ([responseObject[@"status"] isEqual:@2]) { // 注册/登录成功 完善资料
            
            PersonModel *model = [PersonModel yy_modelWithJSON:responseObject[@"data"][@"userinfo"]];
            [InfoCache archiveObject:model toFile:Person];
            
            [LoginModel shareLogin].token = responseObject[@"data"][@"token"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"token"], @"token");
            
            [LoginModel shareLogin].role = responseObject[@"data"][@"userinfo"][@"role_id"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"role_id"], @"role");
            
            [LoginModel shareLogin].uid = responseObject[@"data"][@"userinfo"][@"id"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"id"], @"uid");
            
            [LoginModel shareLogin].username = responseObject[@"data"][@"userinfo"][@"username"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"username"], @"username");
            
            //            [LoginModel shareLogin].name = responseObject[@"data"][@"userinfo"][@"name"];
            SAVEDATAINUSERDEFAULT(responseObject[@"data"][@"userinfo"][@"name"], @"name");
            
            YYEMoreInfoViewController *infoVC = [[YYEMoreInfoViewController alloc]init];
            [self.navigationController pushViewController:infoVC animated:YES];
            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
